package IO;
//io 全部方法
import java.io.*;
import java.nio.charset.Charset;
import java.util.Locale;

public class FileManager {
    public static void main(String[] args) throws FileNotFoundException {
        /*
        try {
            file1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(file1.isFile());
        System.out.println(file1.isDirectory());
        System.out.println(file1.getName());
        System.out.println(file1.getPath());
        System.out.println(  file1.getAbsolutePath());
*/
      //  findFile(new File("D:\\5E\\locales"), ".pak");
      //  copyFile("C:\\Users\\Administrator\\Desktop\\File\\1207.txt","C:\\Users\\Administrator\\Desktop\\新建文件夹 (2)\\1207.txt");
          InputStream I = new FileInputStream("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
          in(I);
    }

    private static void in(InputStream in) {
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹\\1207.txt");
            try {
                Reader reader = new InputStreamReader(in, Charset.defaultCharset());
                char[] chars = new char[1024];
                int len=-1;
               while (((len = reader.read(chars))!= -1)){
                    System.out.println(new String(chars,0,len));
                }
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


    }//字节转字符  打印文本不建议使用
    private static void charInput() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹\\1207.txt");
        try {
            Reader reader = new FileReader(file);
            char[] chars = new char[1];
            int len = -1;
            StringBuilder sb = new StringBuilder();
            while ((len = reader.read(chars)) != -1) {
                String tt = new String(chars);
                sb.append(tt);
            }
            System.out.println(sb);
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }//字符输入流直接用Reader     打印文本非常建议使用
    private static void byteInput() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            InputStream reader = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = reader.read(bytes)) != -1) {
                System.out.println(new String(bytes,0,len));
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }//字节输入流用InputStream指向FileInputStream    如果是打印文本,则不建议使用

    private static void byteOutput() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            OutputStream os = new FileOutputStream(file, true);
            String xr = "春花到我家\r\n";
            os.write(xr.getBytes());
            System.out.println("write success");
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }//字节输出流
    private static void charOutput() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            Writer writer = new FileWriter(file, true);
            String xr = "喜欢喜欢喜欢喜\r\n";
            writer.write(xr);
            System.out.println("write success");
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }//字符输出流
    private static void out() {
        File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹\\1207.txt");
        try {
            OutputStream os = new FileOutputStream(file, true);
            String wt = "小荷才露尖尖角\r\n";
            os.write(wt.getBytes());
            System.out.println("write success");
            os.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }//字符转字节输出流

    private static void findFile(File src, String ext) {
        if (src == null) return;
        if (src.isDirectory()) {
            File[] files = src.listFiles();
            if (files != null) {
                for (File f : files) {
                    findFile(f, ext);
                }
            }
        }else {
                String name = src.getName().toLowerCase();
                if (name.endsWith(ext)) {
                    System.out.println(src.getAbsolutePath());
                }
            }

    } //指定目录下查找文件

    private static void copyFile(String src,String target){
        File srcFile = new File(src);
        File targetFile = new File(target);
        try {
            targetFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Reader in = null ;
        Writer out = null ;

        try {
            in = new FileReader(srcFile);
            out = new FileWriter(targetFile);
            char[] chars = new char[1];
            int len = -1;
           while ((len = in.read(chars))!=-1){
               out.write(chars);
           }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
             {
                try {
                    if(in!=null)in.close();
                    if(out!=null)out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }  //从一个目录下复制一个文件,到另一个目录下创建一个新文件接收
}
